package cn.zyq.ykb.service.product.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.zyq.ykb.beans.product.ProductLink;
import cn.zyq.ykb.beans.product.ProductOrder;
import cn.zyq.ykb.beans.product.ProductType;
import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.mapper.product.ProductLinkMapper;
import cn.zyq.ykb.mapper.product.ProductOrderMapper;
import cn.zyq.ykb.mapper.product.ProductTypeMapper;
import cn.zyq.ykb.mapper.user.UserMapper;
import cn.zyq.ykb.system.common.BaseService;
import cn.zyq.ykb.system.enums.core.OrderTypeEnum;
import cn.zyq.ykb.system.enums.core.UserPositionEnum;
import cn.zyq.ykb.system.exception.BusinessException;
import com.alibaba.druid.sql.visitor.functions.If;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.product.ProductMapper;
import cn.zyq.ykb.beans.product.Product;
import cn.zyq.ykb.service.product.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 商品Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-11-02
 */
@Service
@Slf4j
public class ProductServiceImpl  extends BaseService implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Autowired
    private ProductLinkMapper productLinkMapper;
    @Autowired
    private ProductOrderMapper productOrderMapper;

    public void checkProductPriceByUser(Product product, User user){
        User user1 = this.userMapper.selectByPrimaryKey(user.getUserID());
        BigDecimal originalPrice = product.getPrice();
        if (user1.getPosition().equals(UserPositionEnum.PT.getValue())
                || user1.getPosition().equals(UserPositionEnum.TDZ.getValue())
                || user1.getPosition().equals(UserPositionEnum.ZS.getValue())
                || user1.getPosition().equals(UserPositionEnum.TYB.getValue()) ){
            product.setYhPrice(originalPrice);

        }else if (user1.getPosition().equals(UserPositionEnum.FWS.getValue())){
            //服务商单价是7折
            double div = 0.7;
            BigDecimal mul = originalPrice.multiply(new BigDecimal(div)).setScale(1,BigDecimal.ROUND_HALF_UP);
            if (mul.compareTo(BigDecimal.ZERO) > 0 ){
                product.setYhPrice(mul);
            }
        }else if (user1.getPosition().equals(UserPositionEnum.GR.getValue()) ){
            //个人单价是8折
            double div = 0.8;
            BigDecimal mul = originalPrice.multiply(new BigDecimal(div)).setScale(1,BigDecimal.ROUND_HALF_UP);
            if (mul.compareTo(BigDecimal.ZERO) > 0 ){
                product.setYhPrice(mul);
            }
        }else{
            throw new BusinessException("获取用户角色异常");
        }

    }




    /**
     * 查询商品
     *
     * @param productID 商品主键
     * @return 商品
     */
    @Override
    public Product selectProductByProductID(Integer productID) {
        Product product = productMapper.selectByPrimaryKey(productID);
        if (product.getDeleted() == 1) {
            return null;
        }
        return product;
    }

    /**
     * 查询商品
     *
     * @param serviceID 业务ID
     * @return 商品
     */
    @Override
    public Product selectProductByServiceID(String serviceID) {
        Product param = new Product();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.productMapper.selectOne(param);
    }

    /**
     * 查询商品列表
     *
     * @param pageParam
     * @param param
     * @return 商品
     */
    @Override
    public PageInfo<Product> selectProductList(PageParam pageParam, User param) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<Product> list = this.productMapper.list(pageParam.getKv());
        for (Product product : list) {
            User user = this.userMapper.selectByPrimaryKey(param.getUserID());
            this.checkProductPriceByUser(product,user);
            ProductType productType = this.productTypeMapper.selectByPrimaryKey(product.getProductTypeID());
            product.setProductType(productType);
            ProductLink link = new ProductLink();
            link.setProductID(product.getProductID());
            link.setDeleted(0);
            List<ProductLink> productLink = this.productLinkMapper.select(link);
            product.setProductLink(productLink);
            ProductOrder order = new ProductOrder();
            order.setUserID(user.getUserID());
            order.setProductID(product.getProductID());
            order.setDeleted(0);
            order.setOrderState(OrderTypeEnum.FB.getValue());
            ProductOrder order1 = this.productOrderMapper.selectOne(order);
            if (BeanUtil.isNotEmpty(order1)){
                product.setIsPay(true);
                product.setProductOrder(order1);
            }
        }
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Product> selectProductListGL(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<Product> list = this.productMapper.list(pageParam.getKv());
        for (Product product : list) {
            ProductType productType = this.productTypeMapper.selectByPrimaryKey(product.getProductTypeID());
            product.setProductType(productType);
            ProductLink link = new ProductLink();
            link.setProductID(product.getProductID());
            link.setDeleted(0);
            List<ProductLink> productLink = this.productLinkMapper.select(link);
            product.setProductLink(productLink);
        }
        return new PageInfo<>(list);
    }

    @Override
    public Product selectProductByProductIDH5(Integer productID,User user) {
        Product product = productMapper.selectByPrimaryKey(productID);
        User user1 = this.userMapper.selectByPrimaryKey(user.getUserID());
        this.checkProductPriceByUser(product,user1);
        if (product.getDeleted() == 1) {
            return null;
        }
        ProductOrder order = new ProductOrder();
        order.setUserID(user1.getUserID());
        order.setProductID(productID);
        order.setOrderState(OrderTypeEnum.FB.getValue());
        order.setDeleted(0);
        ProductOrder order1 = this.productOrderMapper.selectOne(order);
        if (BeanUtil.isNotEmpty(order1)){
            product.setIsPay(true);
            product.setProductOrder(order1);
        }
        ProductLink link = new ProductLink();
        link.setProductID(productID);
        link.setDeleted(0);
        List<ProductLink> select = this.productLinkMapper.select(link);
        product.setProductLink(select);
        return product;
    }

    /**
     * 查询商品列表
     *
     * @param pageParam
     * @return 商品
     */
    @Override
    public List<Product> selectProductAllList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<Product> list = this.productMapper.list(pageParam.getKv());
        return list;
    }

    /**
     * 新增商品
     *
     * @param param 商品
     * @return 结果
     */
    @Override
    public int insertProduct(Product param) {
        log.info("【新增参数】，{}",param.toString());
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        int i = this.productMapper.insertSelective(param);
        log.info("====================================================",i);
        if (i == 0){
            throw new BusinessException("新增商品失败");
        }

        if (StrUtil.isEmpty(param.getLink())){
            throw new BusinessException("视频必传");
        }
        ProductLink productLink = new ProductLink();
        productLink.setLink(param.getLink());
        if (StrUtil.isNotEmpty(param.getFileLink())){
            productLink.setFileLink(param.getFileLink());
        }
        productLink.setProductID(param.getProductID());
//        productLink.setTryTime(param.getTryTime());

        this.setCreateBaseData(productLink,SecurityUtils.getLoginUser().getSysAdmin());
        return this.productLinkMapper.insertSelective(productLink);
    }

    /**
     * 修改商品
     *
     * @param param 商品
     * @return 结果
     */
    @Override
    public int updateProduct(Product param) {
        ProductLink link = new ProductLink();
        link.setProductID(param.getProductID());
        link.setDeleted(0);
        List<ProductLink> productLink = this.productLinkMapper.select(link);
        for (ProductLink productLink1 : productLink) {
            this.productLinkMapper.delete(productLink1);
        }
        ProductLink Link = new ProductLink();
        Link.setLink(param.getLink());
        Link.setFileLink(param.getFileLink());
        Link.setProductID(param.getProductID());
//        link.setTryTime(param.getTryTime());
        this.setCreateBaseData(Link,SecurityUtils.getLoginUser().getSysAdmin());
        this.productLinkMapper.insertSelective(Link);

        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.productMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除商品
     *
     * @param productIDs 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteProductByProductIDs(Integer[] productIDs) {
        int i = 0;
        for (Integer productID : productIDs) {
            i += this.deleteProductByProductID(productID);
        }
        return i;
    }

    /**
     * 删除商品信息
     *
     * @param productID 商品主键
     * @return 结果
     */
    @Override
    public int deleteProductByProductID(Integer productID) {
        Product product = this.selectProductByProductID(productID);
        if (product == null) {
            return 0;
        } else {
            Product storeInfo = new Product();
            storeInfo.setProductID(productID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.productMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }


}
